package com.vandagroup.portal.oper;

/**
 * @author po tools
 * 
 * @date Jan.20,2005
 */

import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.vandagroup.common.bo.UserContext;
import com.vandagroup.common.resources.SysPara;
import com.vandagroup.common.util.InputSet;
import com.vandagroup.common.webapp.ActiveAction;
import com.vandagroup.common.webapp.ResourceReader;

public class OperAction extends ActiveAction {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {

		if (task == null || task.equals("")) {
			return (forwardErrorPage(request, mapping, ResourceReader
					.getMessageFromResource("error.paralose")));
		}
		if (step == null || step.equals("")) {
			return (forwardErrorPage(request, mapping, ResourceReader
					.getMessageFromResource("error.paralose")));
		}
		try {

			if (task.equals("list")) {
				return doList(mapping, form, request, response, user);
			} else if (task.equals("new")) {
				return doAdd(mapping, form, request, response, user);
			} else if (task.equals("edit")) {
				return doEdit(mapping, form, request, response, user);
			} else if (task.equals("view")) {
				return doView(mapping, form, request, response, user);
			} else if (task.equals("delete")) {
				return doDelete(mapping, form, request, response, user);
			} else if (task.equals("query")) {
				return doQuery(mapping, form, request, response, user);
			} else {
				return (forwardErrorPage(request, mapping, ResourceReader
						.getMessageFromResource("error.paralose")));
			}
		} catch (Exception e) {
			log.error("Exception:", e);
			return (forwardErrorPage(request, mapping, ResourceReader
					.getMessageFromResource("error.portal.oper"), e));
		}
	}

	public ActionForward doAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		OperBO bo = new OperBO();
		String childOrgIDs = request.getParameter("childOrgIDs");
		String currOrgName = request.getParameter("currOrgName");
		String strPageNO = request.getParameter("pageNO");
		String OrgId = request.getParameter("orgID");
		if (childOrgIDs.startsWith("'")) {
			request.setAttribute("childOrgIDs", URLEncoder.encode(childOrgIDs));
		} else {

			request.setAttribute("childOrgIDs", childOrgIDs);
		}
		request.setAttribute("currOrgName", currOrgName);
		request.setAttribute("pageNO", strPageNO);
		request.setAttribute("orgID", OrgId);
		OperFormBean bean = (OperFormBean) form;
		InputSet set = new InputSet(bo.getRoles(bean.getOrgID()));
		set.setType("checkbox");
		HashSet m = bo.getSelRoles(bean);
		Iterator it = m.iterator();
		String map = "";
		while (it.hasNext()) {
			map = map + it.next() + ",";
		}
		request.setAttribute("map", map);
		set.setValues(bo.getSelRoles(bean));
		request.setAttribute("roleID_SET", set);
		request.setAttribute("form", bo.retrieveForm(bean));

		if (step.equals("init")) {
			return (mapping.findForward("add"));
		} else if (step.equals("submit")) {
			try {
				bean.setLanguage(SysPara.LANGUAGE_COMPLEX);
				bean.setStatus(SysPara.STATUS_ENABLE);
				bean.setSysType(SysPara.SYSTEM_PORTAL);
				bean.setOperType(SysPara.OPRTYPE_WORKER);
				bo.add(bean);

				return (new ActionForward(
						"/Oper.do?task=list&step=list&childOrgIDs="
								+ childOrgIDs + "&currOrgName=" + currOrgName
								+ "&pageNO=" + strPageNO));
			} catch (Exception e) {
				log.error("Exception:", e);
				return (forwardErrorPage(request, mapping, ResourceReader
						.getMessageFromResource("error.portal.oper.add"), e));
			}
		}
		return null;

	}

	public ActionForward doView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		String childOrgIDs = request.getParameter("childOrgIDs");
		String currOrgName = request.getParameter("currOrgName");
		String strPageNO = request.getParameter("pageNO");
		try {
			OperFormBean bean = (OperFormBean) form;
			OperBO bo = new OperBO();

			InputSet set = new InputSet(bo.getRoles(bean.getOrgID()));
			set.setType("checkbox");
			set.setValues(bo.getSelRoles(bean));
			request.setAttribute("roleID_SET", set);
			request.setAttribute("form", bo.retrieveForm(bean));
			request.setAttribute("childOrgIDs", childOrgIDs);
			request.setAttribute("currOrgName", currOrgName);
			request.setAttribute("pageNO", strPageNO);
			return (mapping.findForward("view"));
		} catch (Exception e) {
			log.error("Exception:", e);
			return (forwardErrorPage(request, mapping, ResourceReader
					.getMessageFromResource("error.portal.oper.view"), e));
		}
	}

	public ActionForward doEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		String childOrgIDs = request.getParameter("childOrgIDs");
		String currOrgName = request.getParameter("currOrgName");
		String strPageNO = request.getParameter("pageNO");
		if (step.equals("init")) {
			try {
				OperFormBean bean = (OperFormBean) form;
				OperBO bo = new OperBO();
				InputSet set = new InputSet(bo.getRoles(bean.getOrgID()));
				set.setType("checkbox");
				HashSet m = bo.getSelRoles(bean);
				Iterator it = m.iterator();
				String map = "";
				while (it.hasNext()) {
					map = map + it.next() + ",";
				}
				request.setAttribute("map", map);
				set.setValues(bo.getSelRoles(bean));
				request.setAttribute("roleID_SET", set);
				request.setAttribute("form", bo.retrieveForm(bean));
				request.setAttribute("childOrgIDs", childOrgIDs);
				request.setAttribute("currOrgName", currOrgName);
				request.setAttribute("pageNO", strPageNO);
				return (mapping.findForward("edit"));
			} catch (Exception e) {
				log.error("Exception:", e);
				return (forwardErrorPage(request, mapping, ResourceReader
						.getMessageFromResource("error.portal.oper.view"), e));
			}

		} else if (step.equals("submit")) {
			try {			    
			    if(((OperFormBean)form).getLoginID().indexOf("admin") != -1) {
			        throw new Exception("修改的操作員登錄代碼不能包含“admin”字串，此字串為系統保留字串！");
			    }
				OperBO bo = new OperBO();
				bo.update((OperFormBean) form);

				return (new ActionForward("/Oper.do?task=list&step=init"));
			} catch (Exception e) {
				log.error("Exception:", e);
				return (forwardErrorPage(
						request,
						mapping,
						ResourceReader
								.getMessageFromResource("error.portal.oper.updatepwd.add"),
						e));
			}
		}
		if (step.equals("update")) {
			try {
				OperBO bo = new OperBO();
				bo.updatePwd((OperFormBean) form);
				return (new ActionForward("/Oper.do?task=list&step=update"));
			} catch (Exception e) {
				log.error("Exception:", e);
				return (forwardErrorPage(request, mapping, ResourceReader
						.getMessageFromResource("error.portal.oper.modi"), e));
			}
		}
		return null;
	}

	public ActionForward doList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		//System.out.println(step);
		//System.out.println(task);
		if (step.equals("init")) {
			return (mapping.findForward("list"));
		} else if (step.equals("list")) {

			String strPageNO = (String) request.getParameter("pageNO");
			int pageNO = strPageNO == null ? Integer.parseInt("1") : Integer
					.parseInt(strPageNO);
			try {
				OperBO bo = new OperBO();
				OperFormBean bean = (OperFormBean) form;
				String currOrgName = request.getParameter("currOrgName");
				String childOrgIDs = request.getParameter("childOrgIDs");
				if (request.getParameter("OrgID") != null)
					bean.setOrgID(request.getParameter("OrgID"));
				com.vandagroup.common.util.Result t = bo.query(pageNO, bean,
						childOrgIDs);
				request.setAttribute("result", t);
				request.setAttribute("form", bean);
				request.setAttribute("childOrgIDs", childOrgIDs);
				request.setAttribute("currOrgName", currOrgName);
				request.setAttribute("pageNO", strPageNO);
				return (mapping.findForward("operlist"));
			} catch (Exception e) {
				log.error("Exception:", e);
				return (forwardErrorPage(request, mapping, ResourceReader
						.getMessageFromResource("error.portal.oper.search"), e));
			}
		}
		if (step.equals("update")) {

			try {
				OperBO bo = new OperBO();
				OperFormBean bean = (OperFormBean) form;
				request.setAttribute("form", bean);
				return (mapping.findForward("update"));
			} catch (Exception e) {
				log.error("Exception:", e);
				return (forwardErrorPage(request, mapping, ResourceReader
						.getMessageFromResource("error.portal.oper.search"), e));
			}
		} else {
			return (forwardErrorPage(request, mapping, ResourceReader
					.getMessageFromResource("error.steplose")));
		}

	}

	public ActionForward doDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		try {
			OperFormBean bean = (OperFormBean) form;
			OperBO bo = new OperBO();
			int rtn = bo.remove(request.getParameterValues("selOperID"));
			return (new ActionForward("/Oper.do?task=list&step=list"));
		} catch (Exception e) {
			log.error("Exception:", e);
			return (forwardErrorPage(request, mapping, ResourceReader
					.getMessageFromResource("error.portal.oper.del"), e));
		}

	}

	public ActionForward doQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		if (step.equals("init")) {

			return (mapping.findForward("query"));
		}
		return null;
	}

}
